package com.nf.health.app.healthstate.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.SchemeDetailAdapter;
import com.nf.health.app.models.SchemeDetails;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ScreentUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

public class MyPlanDetailsActivity extends BaseActivity implements OnClickListener {
	private List<String> aimList;
	private TextView total_num, total_count, tv_content, remind_content;
	private SchemeDetailAdapter adapter;
	private ListView lv_content;
	private ImageView home_person;
	private Button cancle_plan;
	private String del;
	private ProgressBar pro_view;
	private int width = 0;
	private TranslateAnimation animation, animation2;

	private TextView titlebar_title_tv;
	private View rl_remind;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_my_plan_details);
		
		initView();
		initData();
		initListener();
	}

	private void initView() {
		titlebar_title_tv = (TextView) findViewById(R.id.titlebar_title_tv);
		total_num = (TextView) findViewById(R.id.total_num);
		total_count = (TextView) findViewById(R.id.total_count);
		tv_content = (TextView) findViewById(R.id.tv_content);
		remind_content = (TextView) findViewById(R.id.friendship_content);
		home_person = (ImageView) findViewById(R.id.home_person);
		lv_content = (ListView) findViewById(R.id.lv_content);
		cancle_plan = (Button) findViewById(R.id.cancle_plan);
		pro_view = (ProgressBar) findViewById(R.id.pro_view);		
	}

	private void initData() {
		aimList = new ArrayList<String>();
		adapter = new SchemeDetailAdapter(this, aimList);
		lv_content.setAdapter(adapter);
		ScreentUtils.setListViewHeightBasedOnChildren(lv_content);
		rl_remind = ViewUtil.getView(this, R.id.rl_remind);
		// 初始化动画
		DisplayMetrics metric = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(metric);
		width = metric.widthPixels;
		animation = new TranslateAnimation(0, (width - DensityUtil.dip2px(this,56)), 0, 0);
		animation.setDuration(3000);
		animation.setRepeatCount(0);
		String schemeid = getIntent().getStringExtra("schemeid");
		if(TextUtils.isEmpty(schemeid)){
			showToast("请求数据失败!");
			return;
		}
		provider.requestMyPlanSchemeDetails(schemeid, "SchemeDetails");
	}


	private void initListener() {
		cancle_plan.setOnClickListener(this);
		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation arg0) {

			}

			@Override
			public void onAnimationRepeat(Animation arg0) {

			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				pro_view.clearAnimation();
				home_person.startAnimation(animation2);
			}
		});
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);

		if (actionName.equals("SchemeDetails")) {
			SchemeDetails result = (SchemeDetails) object;
			if (result.getAim().size() > 0) {
				for (int i = 0; i < result.getAim().size(); i++) {
					aimList.add(result.getAim().get(i).getAim_person());
				}
			}
			pro_view.setProgress(pro_view.getMax() / 2);
			

			animation2 = new TranslateAnimation(width - DensityUtil.dip2px(MyPlanDetailsActivity.this,56),
					((width - DensityUtil.dip2px(MyPlanDetailsActivity.this,24)) * (pro_view.getProgress()) / 100)
							- DensityUtil.dip2px(MyPlanDetailsActivity.this,24), 0, 0);
			animation2.setDuration(3000);
			animation2.setRepeatCount(0);
			animation2.setFillAfter(true);
			home_person.startAnimation(animation);

			titlebar_title_tv.setText(result.getDetail());
			total_num.setText("总共" + result.getNum() + "人参加");
			total_count.setText("我参加了" + result.getPerson_num() + "次");
			if (result.getDescribe() != null && result.getDescribe() != "") {
				tv_content.setVisibility(View.VISIBLE);
				tv_content.setText(result.getDescribe());
			} else {
				tv_content.setVisibility(View.GONE);
			}
			if (aimList.size() > 0) {
				adapter.notifyDataSetChanged();
				ScreentUtils.setListViewHeightBasedOnChildren(lv_content);
			}
			if (TextUtils.isEmpty(result.getFriendship())) {
				rl_remind.setVisibility(View.GONE);
			} else {
				rl_remind.setVisibility(View.VISIBLE);
				remind_content.setText(result.getFriendship());
			}
			if ("y".equals(result.getDel())) {
				cancle_plan.setVisibility(View.VISIBLE);
				cancle_plan.setText("取消计划");
				del = "y";
			} else if (result.getDel().equals("n")) {
				cancle_plan.setVisibility(View.VISIBLE);
				cancle_plan.setText("添加计划");
				del = "n";
			}
			
		} else if (actionName.equals("delSchemeById")) {
			setResult(101);
			finish();
		} else if (actionName.equals("addToUser")) {
			setResult(102);
			finish();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancle_plan:
			if (del.equals("y")) {
				provider.requestDelSchemeByUserid(getIntent().getStringExtra("schemeid"), "delSchemeById");
			} else if (del.equals("n")) {
				provider.requestAddToUser(getIntent().getStringExtra("schemeid"), "addToUser");
			}
			break;
		}
		super.onClick(v);
	}
}
